#requires -version 2

Function TryPowerShell {
    param (
        $fileName=$(throw ''),
        $snapins
    )

    if(-Not(Test-Path $filename)) {
        write-host -ForegroundColor Red "$($filename) not found"
        return
    }
    
    if($snapins) {
        Add-PSSnapin $snapins -ErrorAction SilentlyContinue
    }
    
    $global:WindowTitle = "Try PowerShell"
    $global:targetFileName=$fileName

    Function Global:Get-CommandCtl { Select-BootsElement $global:WindowTitle 'Command' }
    Function Global:Get-OutputCtl  { Select-BootsElement $global:WindowTitle 'Output' }

    Function global:txt2flow {
        param($fileName)
        
        $pscode = $false
        $global:code = ""
        
        switch (Get-Content $fileName) {
            {$_.StartsWith('h1')} { Paragraph -TextAlignment Center -LineHeight 3 -FontSize 22 -Background Black -Foreground White $_.Substring(3) }
            {$_.StartsWith('h2')} { Paragraph -LineHeight 1 -FontSize 18  $_.Substring(3) }
            {$_.StartsWith('h3')} { Paragraph -LineHeight 1 -FontSize 15 -Foreground White -Background Blue ( Bold (Italic $_.Substring(3) ) ) }
            {$_.StartsWith('.pscode')} { $pscode = $true }
            {$_.StartsWith('.epscode')} {                
                Paragraph { 
                    Button -Height 28 -Content 'ps >' -Tag $global:code -On_Click { 
                        $cmd = Global:Get-CommandCtl
                        $lblOutput = Global:Get-OutputCtl
                        
                        $cmd.Text = $this.Tag.Trim()
                        if($cmd.Text) {
                            try { 
                                $lblOutput.Text = (Invoke-Expression $cmd.Text -ErrorVariable Global:Err | Out-String -Width 80 )
                            } catch {
                                $lblOutput.Text = $Global:Err[0]
                            }
                        }
                    }
                } 
                
                $pscode = $false
                $global:code = ""
            }
            
            default { 
                if($pscode) { 
                    $global:code += "$($_)`r`n" 
                    Paragraph -LineHeight 1 -Foreground Green -FontFamily "Lucida Console" -FontSize 14 "PS > $_"
                } else {
                    Paragraph -LineHeight 1 $_
                }
            }
        }
    }
    
    Boots -Title $global:WindowTitle -Width 900 -Height 600 -Async -WindowStartupLocation CenterScreen {
        GridPanel -ColumnDefinitions @(
            ColumnDefinition -Width 400
            ColumnDefinition -Width 100*
        ) -RowDefinitions @(
            RowDefinition -Height 50*
            RowDefinition -Height Auto
            RowDefinition -Height 50*
            RowDefinition -Height Auto
        ) {
            Border -BorderThickness 2 -BorderBrush Black -Margin 3 {
                StackPanel -Orientation Horizontal {
                    
                    $h = @{
                        'http://dougfinke.com/blog/'="Development In A Blink" 
                        'http://trypowershell.codeplex.com/'="Try PowerShell Source Code"
                        'http://powerboots.codeplex.com/'="PowerBoots"                        
                    }
                    
                    $h.GetEnumerator() |
                        ForEach {
                            TextBlock -Margin 5 { 
                                Hyperlink -NavigateUri $_.Key $_.Value -On_RequestNavigate { 
                                    [Diagnostics.Process]::Start($_.Uri.AbsoluteUri) 
                                }
                            }
                        }
                }
            } | Set-DependencyProperty -Passthru "Grid.Row" 3 | Set-DependencyProperty -Passthru "Grid.ColumnSpan" 2
            
            FlowDocumentReader -'Grid.Row' 0 -'Grid.RowSpan' 3 -'Grid.Column' 0 -ViewingMode Scroll -Document {
                FlowDocument -FontFamily Arial -ColumnWidth 300 -IsHyphenationEnabled -IsOptimalParagraphEnabled {
                    global:txt2flow $global:targetFileName
                }
            }
            
            
            TextBox -Name Command -TextWrapping Wrap -Margin 5 -AcceptsReturn | 
              Set-DependencyProperty -Passthru "Grid.Row" 0 | 
              Set-DependencyProperty -Passthru "Grid.Column" 1
            
            StackPanel -Orientation Horizontal { 
                Button "_Run" -Margin 5 -Height 32 -On_Click {
                    $cmd = Global:Get-CommandCtl
                    $lblOutput = Global:Get-OutputCtl
                    
                    if($cmd.Text) {
                        try { 
                            $lblOutput.Text = (Invoke-Expression $cmd.Text -ErrorVariable Global:Err | Out-String -Width 80 )
                        } catch {
                            $lblOutput.Text = $Global:Err[0]
                        }
                    }
                } 
                
                Button "_Clear" -Margin 5 -Height 32 -On_Click {
                    $cmd = Global:Get-CommandCtl
                    $lblOutput = Global:Get-OutputCtl
                    
                    $cmd.Text = ""                    
                    $lblOutput.Text = ""
                }
                
            } | Set-DependencyProperty -Passthru "Grid.Row" 1 | Set-DependencyProperty -Passthru "Grid.Column" 1
            
             
            TextBox -Name Output -FontFamily 'Lucida Console' -Margin 5 -IsReadOnly -VerticalScrollBarVisibility Auto -HorizontalScrollBarVisibility Auto | 
                Set-DependencyProperty -Passthru "Grid.Row" 2 | 
                Set-DependencyProperty -Passthru "Grid.Column" 1
        }
    }
}

$path = Split-Path (Resolve-Path $MyInvocation.InvocationName)

if( !(Get-Module PowerBoots) ) {
  Import-Module (Join-Path $path .\PowerBoots\PowerBoots)
}

TryPowerShell (Join-Path $path trypowershellTutorial.txt)
#TryPowerShell (Join-Path $path TryPowershellSql.txt) -Snapins ('SqlServerCmdletSnapin100', 'SqlServerProviderSnapin100')